<?php
include_once(dirname(__FILE__).'/../autoload.php');

class Acceptance_Controller_ticketTest extends Lib_AcceptanceTestcase
{
    public function setUp()
    {
        ob_start();
        $_SESSION['user'] = serialize(array('id' => 1));
        Lib_QueryWrapper::initializeTestData();
    }
    public function teardown()
    {
        ob_end_clean();
    }

    

    public function test_load_backlog()
    {
        $expected = json_encode(array(
           'success' => true,
           'message' => null,
            'data' => array(
                array(
                    'id' => '1', 'name' => 'Example ticket', 'description' => 'demo',
                    'reporterId' => '1', 'assigneeId' => null, 'scrumState' => '1',
                    'productId' => null, 'sprintId' => '1', 'originalEstimate' => null,
                    'remaining' => null, 'ticketColor' => '000000', 'comments' => array()
                ),
                array(
                    'id' => '2', 'name' => 'Assigned', 'description' => 'demo',
                    'reporterId' => '1', 'assigneeId' => '3', 'scrumState' => '1',
                    'productId' => null, 'sprintId' => '1', 'originalEstimate' => null,
                    'remaining' => null, 'ticketColor' => 'FFFFFF', 'comments' => array()
                )
            ),
            'total' => 2
        ));

        $controller = self::initController('Ticket',
                array('assignee' => null, 'product' => null, 'sprint' => null),
                array('scrumState' => 1));
        $controller->_load();
        unset($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Loading backlog failed');
    }
    
    public function test_load_assigned()
    {
        $expected = json_encode(array(
           'success' => true,
           'message' => null,
            'data' => array(
                array(
                    'id' => '2', 'name' => 'Assigned', 'description' => 'demo',
                    'reporterId' => '1', 'assigneeId' => '3', 'scrumState' => '1',
                    'productId' => null, 'sprintId' => '1', 'originalEstimate' => null,
                    'remaining' => null, 'ticketColor' => 'FFFFFF', 'comments' => array()
                )
            ),
            'total' => 1
        ));

        $controller = self::initController('Ticket',
                array('assignee' => 3, 'product' => null, 'sprint' => null),
                array('scrumState' => 1));
        $controller->_load();
        unset($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Loading backlog failed');
    }

    public function test_load_waiting()
    {
        $expected = json_encode(array(
           'success' => true,
           'message' => null,
            'data' => array(
                array(
                    'id' => '3', 'name' => 'Waiting...', 'description' => 'For someone to do something',
                    'reporterId' => '1', 'assigneeId' => null, 'scrumState' => '2',
                    'productId' => null, 'sprintId' => '1', 'originalEstimate' => null,
                    'remaining' => null, 'ticketColor' => '000000', 'comments' => array()
                )
            ),
            'total' => 1
        ));

        $controller = self::initController('Ticket',
                array('assignee' => null, 'product' => null, 'sprint' => null),
                array('scrumState' => 2));
        $controller->_load();
        unset($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Loading backlog failed');
    }

    public function test_load_w_comments()
    {
        $expected = json_encode(array(
           'success' => true,
           'message' => null,
            'data' => array(
                array(
                    'id' => '4', 'name' => 'Ticket with comments', 'description' => 'Ya rly',
                    'reporterId' => '1', 'assigneeId' => '1', 'scrumState' => '3',
                    'productId' => null, 'sprintId' => '1', 'originalEstimate' => null,
                    'remaining' => null, 'ticketColor' => '000000',
                    'comments' => array(
                        array('id' => '3', 'userId' => '3', 'timestamp' => '2011-05-10 08:22:33', 'comment' => 'Kolmas testikommentti'),
                        array('id' => '2', 'userId' => '3', 'timestamp' => '2011-05-06 22:44:55', 'comment' => 'Toinen testikommentti'),
                        array('id' => '1', 'userId' => '1', 'timestamp' => '2011-05-05 11:22:33', 'comment' => 'Ensimmäinen testikommentti')
                    )
                )
            ),
            'total' => 1
        ));

        $controller = self::initController('Ticket',
                array('assignee' => 1, 'product' => null, 'sprint' => 1),
                array('scrumState' => 3));
        $controller->_load();
        unset($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Loading backlog failed');
    }
    
    public function test_create_just_name()
    {
        $input = array(
            'name' => 'Acceptance test ticket',
            'description' => '',
            'sprint' => '',
            'product' => '',
            'assignee' => '',
            'originalEstimate' => '',
            'ticketColor' => '000000'
        );
        $expected = json_encode(array(
            'success' => true,
            'message' => 'Ticket #5 Acceptance test ticket added',
            'data' => array(
                'id' => '5',
                'name' => 'Acceptance test ticket',
                'description' => null,
                'reporterId' => '1',
                'assigneeId' => null,
                'scrumState' => '1',
                'productId' => null,
                'sprintId' => null,
                'originalEstimate' => '0',
                'remaining' => '0',
                'ticketColor' => '000000'
            ),
            'total' => 1
        ));
        $controller = self::initController('Ticket', $input);
        $controller->_add();
        unset ($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual);
    }

    public function test_create_multipleStuff()
    {
        $input = array(
            'name' => 'Acceptance test ticket 2',
            'description' => 'Bogus description',
            'sprint' => '',
            'product' => '',
            'assignee' => 2,
            'originalEstimate' => '2 d',
            'ticketColor' => '000F00'
        );
        $expected = json_encode(array(
            'success' => true,
            'message' => 'Ticket #5 Acceptance test ticket 2 added',
            'data' => array(
                'id' => '5',
                'name' => 'Acceptance test ticket 2',
                'description' => 'Bogus description',
                'reporterId' => '1',
                'assigneeId' => '2',
                'scrumState' => '1',
                'productId' => null,
                'sprintId' => null,
                'originalEstimate' => '12',
                'remaining' => '12',
                'ticketColor' => '000F00'
            ),
            'total' => 1
        ));
        $controller = self::initController('Ticket', $input);
        $controller->_add();
        unset ($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual);
    }
}